<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/29 0029
 * Time: 下午 14:17
 */

namespace app\admin\model;

use think\Loader;
use think\Validate;
use think\Db;
use think\Model;
class ProductTypes extends Model
{
    protected  $pk='protype_id';//主键
    protected $table = 'xcx_product_types';
    public function  getPTDate()
    {
        $res=$this->where('protype_pid',1)->where('protype_states',1)->select();
        return $res;
    }
    //增加分类
    public function type_add($data)
    {
        //halt($data);
        $validate = Loader::validate('ProductTypes');

        if(!$validate->check($data)){
            return ['valid'=>0,'msg'=>$validate->getError()];
        }
        $arr['createtime']=time();
        $arr['protype_states']=1;
        $arr['protype_sort']=$data['protype_sort'];
        $arr['protype_name']=$data['protype_name'];
        $arr['protype_describe']=$data['protype_describe'];
        $arr['protype_number']=$data['protype_number'];
        $arr['protype_pid']=$data['protype_pid'];

        //判断是否为外来注册


        //判断名称是否唯一
        $type_yan=$this->where('protype_name',$data['protype_name'])->find();

        if($type_yan['protype_id']!="")
        {
            return ['valid'=>0,'msg'=>'名称重复'];
            exit;
        }

        //halt($user_yan);
        $result = $this->save($arr);

        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>1,'msg'=>'添加失败'];
        }else{
            return ['valid'=>4,'msg'=>'添加成功'];
        }
    }
    /**
     * 分类数据
     */
    public function getAllData()
    {
        $arr=$this->getPTDate();
        //$arr=$this->GetTree($typesdata, 0, 0);
        return $arr;
    }
    public function getTypeDataAll()
    {
        $typesdata=$this->where('protype_states',1)->select();

        $arr=$this->GetTree($typesdata, 1, 0);
        //halt($arr);
        return $arr;
    }
    //无限级分类
    private function GetTree($arr,$pid,$step){
        global $tree;
        foreach($arr as $key=>$val) {
            if($val['protype_pid'] == $pid) {
                $flg = str_repeat('└―',$step);
                $val['protype_name'] = $flg.$val['protype_name'];
                $tree[] = $val;
                $this->GetTree($arr , $val['protype_id'] ,$step+1);
            }
        }
        return $tree;
    }
    //查询分类编号
    public function getTypeNumber($protype_id)
    {
        $res=$this->where('protype_id',$protype_id)->field('protype_pid,protype_number')->find();
        $superior=$this->where('protype_id',$res['protype_pid'])->field('protype_number')->find();
        $number=array(
            'series'=>$res['protype_number'],
            'type'=>$superior['protype_number']
        );
        return $number;
    }
    public function getPareArr($id)
    {

        $data=$this->where('protype_pid',$id)->where('protype_states',1)->select();
        $res=array(
            'data'=>$data,
            'id'=>$id
        );
        return $res;
    }
    //修改信息
    public function getOneInfo($id)
    {
       $res=$this->where('protype_id',$id)->find();
       return $res;
    }
    //修改提交
    public  function edit_type($data)
    {
        $result = $this->save($data,['protype_id'=>$data['protype_id']]);
        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>1,'msg'=>'修改失败'];
        }else{
            return ['valid'=>4,'msg'=>'修改成功'];
        }
    }
    //下架
    public function shelf($data)
    {

        $protype_id=$this->where('protype_pid',$data['id'])->value('protype_id');
        if($protype_id)
        {
            return ['valid'=>1,'msg'=>'请先下架子分类'];
            exit;
        }
        $pspeci_states=$this->where('protype_id',$data['id'])->value('protype_states');

        if($pspeci_states==1)
        {
            $result=$this->where('protype_id',$data['id'])->update(['protype_states'=>0]);
        }else
        {
            $result=$this->where('protype_id',$data['id'])->update(['protype_states'=>1]);
        }
        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>1,'msg'=>'操作失败'];
        }else{
            return ['valid'=>4,'msg'=>'操作成功'];
        }
    }
}